package net.xdclass.controller;

import net.xdclass.controller.req.AccountLoginReq;
import net.xdclass.controller.req.AccountRegisterReq;
import net.xdclass.dto.AccountDTO;
import net.xdclass.interceptor.LoginInterceptor;
import net.xdclass.service.AccountService;
import net.xdclass.util.JsonData;
import net.xdclass.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

// 引入 Knife4j 注解
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;

/**
 * @author 姜阿木
 * @date 2025/5/15 17:28
 */
@RestController
@RequestMapping("/api/account/v1")
@Tag(name = "账户管理接口") // 类级 Tag 注解
public class AccountController {

    @Autowired
    private AccountService accountService;

    /**
     * 注册接口
     */
    @PostMapping("/register")
    @Operation(summary = "用户注册接口")
    public JsonData register(@RequestBody AccountRegisterReq req){
        accountService.register(req);
        return JsonData.buildSuccess();
    }

    /**
     * 头像上传接口
     */
    @PostMapping("/upload_avatar")
    @Operation(summary = "头像上传接口")
    public JsonData uploadAvatar(@RequestParam("file") MultipartFile file){
        String url = accountService.uploadAvatar(file);
        return JsonData.buildSuccess(url);
    }

    /**
     * 登录接口
     */
    @PostMapping("/login")
    @Operation(summary = "用户登录接口")
    public JsonData login(@RequestBody AccountLoginReq req){
        AccountDTO accountDTO = accountService.login(req);

        // 生成token jwt 一般前端存储在localStorage sessionStorage
        String token = JwtUtil.geneLoginJWT(accountDTO);
        return JsonData.buildSuccess(token);
    }

    /**
     * 获取用户详情
     */
    @GetMapping("/detail")
    @Operation(summary = "获取用户详细信息接口")
    public JsonData info(){
        Long AccountId = LoginInterceptor.threadLocal.get().getId();
        AccountDTO accountDTO = accountService.getDetail(AccountId);
        return JsonData.buildSuccess(accountDTO);
    }
}